var STATIC_URL = '/static/';

var swfu;

var queueErrorArray;

var counter;

var swf_settings = {
	debug : false,

	upload_url : '/upload/',
	flash_url : STATIC_URL + 'Flash/swfupload.swf',

	post_params : {
		'csrfmiddlewaretoken':getCookie("csrftoken"),
	},

	file_size_limit : '20 MB',
	file_queue_limit: 20,
	file_post_name: 'fileData',

	button_placeholder_id : 'spanButtonPlaceholder',
	button_text: '选择文件',
	button_text_style: '.whiteFont{ color: #FFFFFF; }',
	button_image_url: STATIC_URL + 'imgs/button_img.png',
	button_width: 65, 
	button_height: 29,
	button_cursor: SWFUpload.CURSOR.HAND,
	button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT, 
	button_text_top_padding : 5,
	button_text_left_padding : 3,

	file_dialog_start_handler : onFileSelectDialogOpen,
	file_dialog_complete_handler : onFileSelectDialogClose,
	file_queued_handler : onFileQueued,
	file_queue_error_handler : onFileQueuedError,
	upload_start_handler : onFileUploadStart,
	upload_progress_handler : onFileUpload,
	upload_complete_handler : onFileUploadComplete,

};



$(function  () {
	swfu = new SWFUpload(swf_settings);
})

function onFileSelectDialogOpen () {
	// 初始化错误文件队列
	if (queueErrorArray) {
		queueErrorArray = null;
	}
	counter = 0;
}

function onFileQueued (file) {
	var swfupload = this;
	var listItem = '<li id="' + file.id + '">';
	listItem += '文件:<em>' + file.name + '</em>(' + Math.round(file.size/1024) + ' KB)';
	listItem += '<div id="info_'+ file.id +'"></div>';
	listItem += '<div id="percent_' + file.id + '"></div>';
	listItem += '</li>';
	$("#uploadQueue").append(listItem);
}

function onFileSelectDialogClose (numSelected,numQueued,numTotalInQueued) {
	counter = numQueued;
	console.log("init---->",counter);
	this.startUpload();
}

function onFileQueuedError (file,errorCode,message) {
	// 当前选择文件第一次出错
	if (!queueErrorArray) {
		queueErrorArray = [];
	}
	var errorFile = {
		file: file,
		code: errorCode,
		error: ''
	};
	switch (errorCode) {
	case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
		errorFile.error = '文件大小超出限制.';
		break;
	case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		errorFile.error = '文件类型受限.';
		break;
	case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		errorFile.error = '文件为空文件.';
		break;
	case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
		errorFile.error = '超出文件数量限制.';
		break;
	default:
		alert('加载入队列出错.');
		break;
	}
	alert(errorFile.error);
	queueErrorArray.push(errorFile)
}


function onFileUploadStart (file) {
	if (file) {
		$("#info_" + file.id).html('上传中...');
		$("#percent_" + file.id).html('0%');
	}
}

function onFileUpload (file,bytesCompleted,bytesTotal) {
	console.log(bytesCompleted + " " + bytesTotal);
	var percentage = Math.round((bytesCompleted / bytesTotal) * 100);
	$("#percent_" + file.id).html(percentage + '%');
}

function onFileUploadComplete (file) {
	this.refreshCookies();
	if (--counter <= 0 ) {
		location.reload();
	}
	$("#info_" + file.id).html('上传成功!');
	console.log("counter---->", counter);
	this.uploadStart();
}

// Get cookie by cookie_name
function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}








